/* --------------------------------------
SUMMARY: 	- Estimating discete choice models
			- Exporting the estimated parameters to a new dataset
AUTHOR: Thimo De Schouwer (KU LEUVEN)
NOTES: 		- Preference / WTP coefficients vary over wage distribution
DATE CREATED: 16/09/2021
STATA VERSION: MP 17.0
--------------------------------------- */

clear all
drop _all

* Part 2 - Estimating preferences for job attributes
cd "${dir}\DataCode\Data\Constructed\LISS"
import delimited Clean_LISS.txt, clear

*=========================*
* 1. Reshaping data 	  *
*=========================*
* Job A characterized by: whA flA tcA mwA mwageA
* Job B characterization differs by experiment
* 2.1 First reshape: one observation per experiment

** JOB A (based on actual job)
forvalues i = 1/10{
	gen tca`i' = telecommutea
	gen wha`i' = workhoursa
	gen fla`i' = flexibilitya
	gen mwa`i' = meaninga
	gen hwagea`i' = hrlywagea
	gen mwagea`i' = monthlywagea
}

** JOB B (differs by experiment)
* 1. Workhours
foreach i in 2 3 4 8 9 10{
	gen whb`i' = workhoursa
}

* 2. Flexibility
foreach i in 1 3 4 5 7 10{
	gen flb`i' = flexibilitya
}

* 3. Meaning
foreach i in 4 5 8 10{
	gen mwb`i' = meaningb
}

foreach i in 1 2 3 6 7 9{
	gen mwb`i' = meaninga
}

* 4. Telecommute
foreach i in 3 7 9 10{
	gen tcb`i' = telecommuteb
}

foreach i in 1 2 4 5 6 8 {
	gen tcb`i' = telecommutea
}

* 5. Monthly Wages & Hourly Wages
forvalues i = 1/10{
	gen mwageb`i' = wage_b`i'
}

forvalues i = 1/10{
	gen hwageb`i' = mwageb`i' / (whb`i'*(52/12))
}


* 6. Choices
forvalues i = 1/10{
	gen choicea`i' = 1 if hce`i' == 1
		replace choicea`i' =  0 if hce`i' == 2
	gen choiceb`i' = 1 if hce`i' == 2
		replace choiceb`i' =  0 if hce`i' == 1
}


reshape long wha whb mwagea mwageb hwagea hwageb mwa mwb tca tcb fla flb choicea choiceb, i(nomem_encr) j(experiment)

* 1.2 Second reshape: one observation per choice
reshape long wh mw tc fl choice hwage mwage, i(nomem_encr experiment) j(choicealt a b) string
sort nomem_encr experiment

* Generating log wages and earnings
gen ln_mwage = ln(mwage)
gen ln_hwage = ln(hwage)

* Binary flexibility and hour categories
recode wh (38 = 0) (32 = 1) (20 = 2)
recode fl (0 = 0) (1 2 3 = 1)

drop if choice == .

*====================================================*
* 2. 	Estimating Discrete Choice Models			 *
*		Base Model: preferences bary only by Gender	 *
*====================================================*
* Declare choice method data
egen id = group(nomem_encr experiment)
cmset nomem_encr experiment choicealt

* Labels
label var wh "Workhours"
label var ln_hwage "Log hourly wage"
label var fl "Schedule Adaptability"
label var tc "Telecommuting"
label var mw "Meaning"

label def whl 0 "Full-Time" 1 "Long Part-Time" 2 "Short Part-Time"
label val wh whl

label def hoursl 1 "Long Part-Time (32h)" 2 "Short Part-Time (20h)" 
label val wh whl

label def fll 1 "Long-term schedule changes" 2 "Short-term schedule changes" 3 "Schedule freedom"
label val fl fll

* Interaction terms: Job Attribute x Young Child
gen child = (nrchild_hh > 0)
	replace child = . if nrchild_hh == .

gen child_wh1 = (child * wh) if wh == 1
	replace child_wh1 = 0 if child_wh1 == .
	
gen child_wh2 = (child * wh) if wh == 2
	replace child_wh2 = 0 if child_wh2 == .
	
gen child_fl = child * fl
gen child_tc = tc * child
gen child_mw = mw * child


label var child_wh1 "Child $\times$ Long PT"
label var child_wh2 "Child $\times$ Short PT"
label var child_tc "Child $\times$ Telecommute"
label var child_mw "Child $\times$ Meaning"
label var child_fl "Child $\times$ Schedule Freedom"


global interactions child_wh1 child_wh2 child_tc child_mw child_fl

* Results: Overall WTP by gender
foreach g in 0 1{
	* Mixed logit model that works but has results like fixed effects conditional logit
	eststo MIX_`g': xi: mixlogit choice ln_hwage $interactions if woman == `g', group(id) cluster(nomem_encr) rand(fl tc mw i.wh) difficult id(nomem_encr)

	/*
	eststo WTP_SE_`g':  nlcom (EUR_WTP_SE_fl_`g': (1 - exp(- (_b[fl]) / _b[ln_hwage]))) ///
				(WTP_SE_tc_`g': (1 - exp(- (_b[tc] / _b[ln_hwage])))) ///
				(WTP_SE_mw_`g': (1 - exp(- (_b[mw] / _b[ln_hwage])))) ///
				(WTP_SE_wh1_`g': (1 - exp(- (_b[_Iwh_1] / _b[ln_hwage])))) ///
				(WTP_SE_wh2_`g': (1 - exp(- (_b[_Iwh_2] / _b[ln_hwage])))), post
				*/
				

		
		est restore MIX_`g'	
		* WTP in Euro: no children
		eststo WTP_SE_nc_`g': nlcom (WTP_SE_nc_fl_`g': (1 - exp(- (_b[fl]) / _b[ln_hwage]))) ///
				(WTP_SE_nc_tc_`g': (1 - exp(- (_b[tc] / _b[ln_hwage])))) ///
				(WTP_SE_nc_mw_`g': (1 - exp(- (_b[mw] / _b[ln_hwage])))) ///
				(WTP_SE_nc_wh1_`g': (1 - exp(- (_b[_Iwh_1] / _b[ln_hwage])))) ///
				(WTP_SE_nc_wh2_`g': (1 - exp(- (_b[_Iwh_2] / _b[ln_hwage])))), post
		
		* WTP in Euro: children
		est restore MIX_`g'
		eststo WTP_SE_c_`g': nlcom (WTP_SE_c_fl_`g': (1 - exp(- (_b[fl] + _b[child_fl]) / _b[ln_hwage]))) ///
				(WTP_SE_c_tc_`g': (1 - exp(- ((_b[tc] + _b[child_tc]) / _b[ln_hwage]) ))) ///
				(WTP_SE_c_mw_`g': (1 - exp(- ((_b[mw] + _b[child_mw]) / _b[ln_hwage]) ))) ///
				(WTP_SE_c_wh1_`g': (1 - exp(- ((_b[_Iwh_1] + _b[child_wh1]) / _b[ln_hwage]) ))) ///
				(WTP_SE_c_wh2_`g': (1 - exp(- ((_b[_Iwh_2] + _b[child_wh2]) / _b[ln_hwage]) ))), post
				

}


** Tables: WTP
esttab WTP_SE_nc_0 WTP_SE_nc_1 using "$dir\GraphsTables\OnlineAppendix\Table_WTP_Mixlogit_NC.txt", tex varwidth(25) modelwidth(15) b(%5.3f) ///
	rename(WTP_SE_nc_fl_1 "Schedule Adaptability" WTP_SE_nc_fl_0 "Schedule Adaptability" WTP_SE_nc_tc_1 Telecommuting WTP_SE_nc_tc_0 Telecommuting WTP_SE_nc_mw_1 Meaning WTP_SE_nc_mw_0 Meaning WTP_SE_nc_wh1_1 "Long Part-Time" WTP_SE_nc_wh1_0 "Long Part-Time" WTP_SE_nc_wh2_1 "Short Part-Time" WTP_SE_nc_wh2_0 "Short Part-Time") ///
	nonote ///
    eqlabels(none) collabels(none) mtitles("Men" "Women") se nogaps nofloat nonumbers replace star(* 0.05)

esttab WTP_SE_c_0 WTP_SE_c_1 using "$dir\GraphsTables\OnlineAppendix\Table_WTP_Mixlogit_C.txt", varwidth(25) modelwidth(15) b(%5.3f) ///
	rename(WTP_SE_c_fl_1 "Schedule Adaptability" WTP_SE_c_fl_0 "Schedule Adaptability" WTP_SE_c_tc_1 Telecommuting WTP_SE_c_tc_0 Telecommuting WTP_SE_c_mw_1 Meaning WTP_SE_c_mw_0 Meaning WTP_SE_c_wh1_1 "Long Part-Time" WTP_SE_c_wh1_0 "Long Part-Time" WTP_SE_c_wh2_1 "Short Part-Time" WTP_SE_c_wh2_0 "Short Part-Time") ///
	addnote("Note: standard errors clustered on the individual level and calculated with delta method.") ///
    eqlabels(none) collabels(none) mtitles("Men" "Women") se nogaps nofloat nonumbers replace star(* 0.05) tex
	
** Tables: Coefficients
esttab MIX_0 MIX_1 using "$dir\GraphsTables\OnlineAppendix\Table_Coeffs_MixLogit.txt" , label ///
	varwidth(25) modelwidth(10) b(%5.3f) noomitted nobase ///
	addnote("Note: standard errors clustered on the individual level.") rename(fl "Schedule Adaptability" tc "Telecommuting" mw "Work Meaning" _Iwh_1 "Long Part-Time" _Iwh_2 "Short Part-Time" ln_hwage "Wages (log)" _cons "Constant")  ///
	eqlabels(none) order("Schedule Adaptability" "Telecommuting" "Work Meaning" "Long Part-Time" "Short Part-Time") collabels(none) mtitles("Men" "Women") se nogaps nofloat nonumbers replace ///
	refcat(child_wh1 "\textbf{Children Interaction}:", nolab) star(* 0.05) tex

*/

